package com.hongkzh.www.view.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;

import com.hongkzh.www.R;
import com.hongkzh.www.other.utils.TitleManager;
import com.hongkzh.www.presenter.BasePresenter;
import com.hongkzh.www.view.iview.BaseIView;

import butterknife.ButterKnife;

import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getStatusBarHeight;

/**
 * mvp activity 基类
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseIView {

    private P presenter;
    protected TitleManager mTiManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mTiManager = new TitleManager(this);
        init();
        setListener();
    }

    protected void setTranslucent(int color) {
        //设置 paddingTop
        ViewGroup rootView = this.getWindow().getDecorView().findViewById(android.R.id.content);
        rootView.setPadding(0, getStatusBarHeight(this), 0, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 以上直接设置状态栏颜色
            this.getWindow().setStatusBarColor(color);
        } else {
            //根布局添加占位状态栏
            ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
            View statusBarView = new View(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(this));
            statusBarView.setBackgroundColor(color);
            decorView.addView(statusBarView, lp);
        }
    }

    //初始化布局
    protected abstract int getLayoutId();

    //初始化控件
    protected abstract void init();

    //初始化监听事件
    protected abstract void setListener();

    //初始化Presenter
    protected void setPresenter(P presenter) {
        this.presenter = presenter;
        presenter.attachView(this);
    }

    //获取Presenter
    public P getPresenter() {
        return presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Presenter解除绑定
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
    }
}
